package tributos.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Set;

import javax.swing.text.MaskFormatter;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

public class Util {

	public static Date stringToDate(String data){
		Date date = null;
		try {  
            DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");  
            date = (java.util.Date)formatter.parse(data);  
        } catch (ParseException e) {              
            e.printStackTrace(); 
        }  
		return date;
	}
	
	
	public static String removerMascara(String dado){
	    return dado.replaceAll("[^0-9]*", "");
	}
	
	public static MaskFormatter inserirMascara(String mascara){
		MaskFormatter mfData = null;
		try {
			mfData = new MaskFormatter(mascara);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return mfData;
	}
	
	public static boolean isNullOrEmpty(Object objeto){
		return objeto == null || objeto.toString() == "";
	}
	
	public static StringBuilder validacaoCampos(Object objeto){
		final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
	    final Set<ConstraintViolation<Object>> violations = validator.validate(objeto);
	    StringBuilder mensagem = new StringBuilder(); 
	    if (!violations.isEmpty()) {
	    	for (ConstraintViolation violation : violations) {
	    		mensagem.append(violation.getMessage().concat("\n"));
	    	}
	    	return mensagem; 
	    }else{
	    	return mensagem;
	    }
	}
	
	public static String getDataAtualFormatada(){
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		Date hoje = Calendar.getInstance().getTime();        
		String reportDate = df.format(hoje);
		return reportDate;
	}
	
	public static String dateToString(Date date){
		DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
		String reportDate = df.format(date);
		return reportDate;
	}
	
	public static String inserirzerosEsquerda(String valueToPad, String filler, int size) {  
		while (valueToPad.length() < size) {  
            valueToPad = filler + valueToPad;  
        }  
        return valueToPad;  
	}
	
	public static String pegarAnoCorrente(){
		 Calendar cal = Calendar.getInstance();  
	        Integer year = cal.get(Calendar.YEAR);  
		return year.toString();
	}
	
	public static String pegarDiaSemanaAno(){
		String retorno = null;
		Calendar cal = Calendar.getInstance();  
		 int day = cal.get(Calendar.DATE);  
	     int month = cal.get(Calendar.MONTH) + 1;  
	     int year = cal.get(Calendar.YEAR);  
	     int dow = cal.get(Calendar.DAY_OF_WEEK);  
	     int dom = cal.get(Calendar.DAY_OF_MONTH);  
	     int doy = cal.get(Calendar.AM_PM); 
	     System.out.println(day+""+month+""+year+""+dow+""+doy);
		return retorno;
	}

}
